import java.util.PriorityQueue;

public class FindKthLargest {
    // leetcode 数组中的第k个最大元素
    // https://leetcode.cn/problems/kth-largest-element-in-an-array/description/?envType=study-plan-v2&envId=top-100-liked
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(k);
        for (int i = 0; i < k; i++) {
            queue.add(nums[i]);
        }
        for (int i = k; i < nums.length; i++) {
            int top = queue.peek();
            if (nums[i] > top) {
                queue.poll();
                queue.add(nums[i]);
            }
        }
        return queue.peek();
    }
}
